<?php
/*
 * Главная страница
 */
class Page extends BasicModel {
	public function __construct(Core $Site) {
		parent::__construct($Site);
	}
	public function makePage() {
		$NowDate = $Cities = $Weathers = $Areas = NULL;
		$tDate = date('Y-m-d');
		// настройка параметров кэширования
		$this->MySQL->CacheSetConfig($this->Config->Cache['main.page']);
		// загрузка кэша
		$this->MySQL->CacheLoad();
		if (!$this->MySQL->CacheIsSet('NOW_DATE')) {
			$this->MySQL->CacheWrite($NowDate, 'NOW_DATE');
		} else {$NowDate = $this->MySQL->CacheGet('NOW_DATE', TRUE);}
		// запись кэша по номой при новом дне
		if ($NowDate != $tDate) {
			$this->MySQL->CacheClear();
			$NowDate = $tDate;
			$this->MySQL->CacheWrite($NowDate, 'NOW_DATE');
		}
		// список областей
		if (!$this->MySQL->CacheIsSet('AREAS')) {
			$Select = $this->MySQL->Select('SELECT `a`.`ID`, `NAME`, `URL` FROM `{p}areas` `a` INNER JOIN `{p}urls` `u` ON `u`.`ID` = `a`.`ID` WHERE `u`.`URL_TYPE` = 1', MySQL::SQuery);
			while(!is_null($Area = $this->MySQL->FetchArray($Select))) {
				$Cities = $this->MySQL->Select('SELECT `AREA_ID`, `c`.`ID`, `NAME`, `URL`, `CITY_TYPE` FROM `{p}cities` `c` INNER JOIN `{p}urls` `u` ON `u`.`ID` = `c`.`ID` WHERE `AREA_ID` = '.$Area['ID'].' AND `u`.`URL_TYPE` = 3 AND `CITY_TYPE` IN (1, 2) ORDER BY `CITY_TYPE` ASC', MySQL::SAll);
				$Areas[] = array(
					'NAME'			=> $Area['NAME'],
					'URL'			=> $Area['URL'],
					'CITIES'	=> $Cities
				);
			}
			if ($Areas) {$this->MySQL->CacheWrite($Areas, 'AREAS');}
		} else {$Areas = $this->MySQL->CacheGet('AREAS', TRUE);}
		// областные города
		if (!$this->MySQL->CacheIsSet('CITIES')) {
			$Cities = $this->MySQL->Select('SELECT `NAME`, `URL` FROM `{p}cities` `c` INNER JOIN `{p}urls` `u` ON `u`.`ID` = `c`.`ID` WHERE `u`.`URL_TYPE` = 3 AND `CITY_TYPE` = 1', MySQL::SAll);
			if ($Cities) {$this->MySQL->CacheWrite($Cities, 'CITIES');}
		} else {$Cities = $this->MySQL->CacheGet('CITIES', TRUE);}
		// погода в городах
		$Dates = NULL;
		if (!$this->MySQL->CacheIsSet('WEATHERS')) {
			$Dates = $this->MySQL->Select('SELECT `DATE` FROM `{p}weathers` WHERE `DATE` >= "'.date('Y-m-d').'" GROUP BY `DATE` LIMIT 5', MySQL::SAll);
			foreach ($Areas as $Area) {
				foreach ($Area['CITIES'] as $City) {
					$Select = $this->MySQL->Select('SELECT * FROM `{p}weathers` WHERE `CITY_ID` = '.$City['ID'].' AND `DATE` >= "'.date('Y-m-d').'" GROUP BY `DATE` LIMIT 5', MySQL::SQuery);
					while(!is_null($Weath = $this->MySQL->FetchArray($Select))) {
						if (!isset($Weathers[$City['ID']])) {
							$Weathers[$City['ID']] = array(
								'NAME'				=> $City['NAME'],
								'CITY_TYPE' => $City['CITY_TYPE'],
								'AREAD_ID'	=> $City['AREA_ID']
							);
						}
						foreach ($Weath as $Index => $Value) {
							if ($Index == 'CITY_ID' || $Index == 'DATE') {continue;}
							if (!isset($Weathers[$City['ID']][$Index])) {$Weathers[$City['ID']][$Index] = array($Value);}
							else {$Weathers[$City['ID']][$Index][] = $Value;}
						}
					}
				}
			}
			if ($Weathers) {$this->MySQL->CacheWrite($Weathers, 'WEATHERS');}
		} else {$Weathers = $this->MySQL->CacheGet('WEATHERS', TRUE);}
		// сохранение кэша
		//$this->MySQL->CacheSave();
		// настройка страницы
		$this->Template->setTitle($this->Language->LText['TITLE_MAIN']); // тайтл
		$this->Template->setLoadPage('main.page'); // шаблон
		$this->Template->setPageElements(array( // элементы страницы
			'AREAS'			=> $Areas,
			'CITIES'		=> $Cities,
			'WEATHERS'	=> $Weathers,
			'DATES'			=> $Dates
		));
		return 0;
	}
}
?>